<?php

namespace common\helpers;

use RongCloud\RongCloud;

/**
 * 数组扩展工具类
 *
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class PushNoticeHelper {

    const APP_KEY = 'mgb7ka1nmd1tg';
    const APP_SECRET = 'XIFwXX2F1xa';
    //消息类型 文本
    const MS_TYPE_TEXT = 'RC:TxtMsg';

    /**
     * 普通类验证码
     * @return boolean
     */
    public static function push($sender, $receiver, $message, $extra = null) {

        if (!$sender) {
            $sender = 1;
        }
        if (empty($receiver) || !$message) {
            Yii::error([
                '接收用户和消息内容不能为空',
                $receiver
            ], __METHOD__);
            return false;
        }
        $status = 200;
        if (YII_ENV_PROD) {
            $model = new RongCloud(static::APP_KEY, static::APP_SECRET);
            $result = $model->getMessage()->System()->send([
                'senderId' => $sender,
                'targetId' => $receiver,
                'objectName' => static::MS_TYPE_TEXT,
                'content' => [
                    'content' => $message,
                    'extra' => $extra
                ],
                'data' => $extra,
            ]);
            $status = ArrayHelper::getValue($result, 'code');
        }
        if ($status != 200) {
            Yii::error([
                '融云推送失败',
                $result
            ], __METHOD__);
            return false;
        }
        return true;
    }

}
